package com.qsd.StudentSystem.controller;

import com.alibaba.fastjson.JSON;
import com.qsd.StudentSystem.service.CourseService;
import com.qsd.common.enums.ResultStatus;
import com.qsd.common.po.Answer;
import com.qsd.common.po.Course;
import com.qsd.common.vo.BaseVo;
import com.qsd.common.vo.DataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("course")
public class CourseController {
	
	@Autowired
	private CourseService courseService;
	
	@GetMapping("search")
	public DataVo<Course> search(Integer id){
		Course course = courseService.search(id);
		if (course != null) {
			return new DataVo<Course>(ResultStatus.SUCCESS, course);
		}else {
			return new DataVo<>(ResultStatus.RESULT_NULL);
		}
	}
	
	@GetMapping("join")
	public BaseVo join(Integer id, HttpServletRequest request) {
		return new BaseVo(courseService.join(id, request));
	}
	
	@GetMapping("exit")
	public BaseVo exit(Integer id, HttpServletRequest request) {
		return new BaseVo(courseService.exit(id, request));
	}
	
	@PostMapping("complete")
	public BaseVo complete(Answer answer, HttpServletRequest request) {
		log.info("answer:{}",answer);
		ResultStatus complete = courseService.complete(answer, request);
		return new BaseVo(complete);
	}

}
